package drds.data_propagate.binlog.event;

import drds.data_propagate.binlog.Buffer;


public final class CreateFileEvent extends LoadEvent {

    /* CF = "Create File" */
    public static final int CF_FILE_ID_OFFSET = 0;
    public static final int CF_DATA_OFFSET = FormatDescriptionEvent.CREATE_FILE_HEADER_LEN;
    protected Buffer buffer;
    protected int blockLen;
    protected long fileId;
    protected boolean initedFromOld;

    public CreateFileEvent(Header header, Buffer buffer, FormatDescriptionEvent formatDescriptionEvent) {
        super(header, buffer, formatDescriptionEvent);

        final int commonHeaderLength = formatDescriptionEvent.commonHeaderLength;
        final int loadEventPostHeaderLength = formatDescriptionEvent.eventPostHeaderLength[LOAD_EVENT - 1];
        final int createFileEventPostHeaderLength = formatDescriptionEvent.eventPostHeaderLength[CREATE_FILE_EVENT - 1];

        copyLogEvent(buffer,
                ((header.eventType == LOAD_EVENT) ? (loadEventPostHeaderLength + commonHeaderLength) : (commonHeaderLength + loadEventPostHeaderLength + createFileEventPostHeaderLength)),
                formatDescriptionEvent);

        if (formatDescriptionEvent.binlogVersion != 1) {
            fileId = buffer.getUint32(commonHeaderLength + loadEventPostHeaderLength + CF_FILE_ID_OFFSET);
            /*
             * Note that it's ok to use get_data_size() below, because it is
             * computed with values we have already read from this event
             * (because we called copy_log_event()); we are not using slave's
             * format info to decode master's format, we are really using
             * master's format info. Anyway, both formats should be identical
             * (except the common_header_len) as these Load events are not
             * changed between 4.0 and 5.0 (as logging of LOAD DATA INFILE does
             * not use Load_log_event in 5.0).
             */
            blockLen = buffer.limit() - buffer.position();
            this.buffer = buffer.duplicate(blockLen);
        } else {
            initedFromOld = true;
        }
    }

    public final long getFileId() {
        return fileId;
    }

    public final Buffer getBuffer() {
        return buffer;
    }

    public final byte[] getData() {
        return buffer.getData();
    }
}
